package com.example.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

	// 定义底部三个控件
	private LinearLayout nearbyLayout;
	private LinearLayout contactLayout;
	private LinearLayout personLayout;

	// 定义底部三个控件的文本控件
	private TextView nearbyText;
	private TextView contactText;
	private TextView personText;

	private FragmentManager fragmentManager;

	private static final String FRAGMENTTAG1 = "fragmentTag1";
	private static final String FRAGMENTTAG2 = "fragmentTag2";
	private static final String FRAGMENTTAG3 = "fragmentTag3";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		setupViews();
		Fragment fragment = new Fragment01();
		// 设置默认fragment
		changViewContent(fragment, FRAGMENTTAG1);
		nearbyText.setTextColor(getResources().getColor(R.color.red));
	}

	private void setupViews() {
		// 取出底部三个控件布局
		nearbyLayout = (LinearLayout) findViewById(R.id.linearlayout_nearby);
		contactLayout = (LinearLayout) findViewById(R.id.linearlayout_contact);
		personLayout = (LinearLayout) findViewById(R.id.linearlayout_person);

		// 取出底部三个控件的TextView
		nearbyText = (TextView) findViewById(R.id.main_textview_nearby);
		contactText = (TextView) findViewById(R.id.main_textview_contact);
		personText = (TextView) findViewById(R.id.main_textview_person);

		// 设置底部三个控件监听
		MyLayoutOnclickListenet myLayoutOnclickListenet = new MyLayoutOnclickListenet();
		nearbyLayout.setOnClickListener(myLayoutOnclickListenet);
		contactLayout.setOnClickListener(myLayoutOnclickListenet);
		personLayout.setOnClickListener(myLayoutOnclickListenet);
		fragmentManager = getSupportFragmentManager();
	}

	private class MyLayoutOnclickListenet implements OnClickListener {

		public void onClick(View v) {

			int id = v.getId();
			Fragment mFragment;
			switch (id) {
			case R.id.linearlayout_nearby:
				mFragment = fragmentManager.findFragmentByTag(FRAGMENTTAG1);
				if (mFragment != null) {
					return;
				}
				mFragment = new Fragment01();
				changViewContent(mFragment, FRAGMENTTAG1);
				nearbyText.setTextColor(getResources().getColor(R.color.red));
				mFragment = null;
				break;
			case R.id.linearlayout_contact:
				mFragment = fragmentManager.findFragmentByTag(FRAGMENTTAG2);
				if (mFragment != null) {
					return;
				}
				mFragment = new ContactFragemntActivity01();
				changViewContent(mFragment, FRAGMENTTAG2);
				contactText.setTextColor(getResources().getColor(R.color.red));
				mFragment = null;
				break;
			case R.id.linearlayout_person:
				mFragment = fragmentManager.findFragmentByTag(FRAGMENTTAG3);
				if (mFragment != null) {
					return;
				}
				mFragment = new Fragment03();
				changViewContent(mFragment, FRAGMENTTAG3);
				personText.setTextColor(getResources().getColor(R.color.red));
				break;
			default:
				break;
			}
		}

	}

	private void resetTextColor() {
		nearbyText.setTextColor(getResources().getColor(R.color.white));
		contactText.setTextColor(getResources().getColor(R.color.white));
		personText.setTextColor(getResources().getColor(R.color.white));
	}

	private void changViewContent(Fragment fragment, String tag) {
		resetTextColor();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.replace(R.id.activity_main_container, fragment, tag);
		transaction.commit();
	}
}